const { doRequest } = require("../utils/request_tool");
const { BaseCommand } = require("./base");

class DingtalkCommand extends BaseCommand {
    async execute() {
        const data = this.selfData;
        const body = {};
        // 要@的目标
        body.at = {
            atMobiles: data.atMobiles || [],
            atUserIds: data.atUserIds || [],
            isAtAll: data.isAtAll || false,
        };
        let atUserSuffix = "";
        atUserSuffix += body.at.atMobiles.map((v) => `@${v}`).join(" ");
        // atUserSuffix += body.at.atUserIds.map(v => `@${v}`).join(" ");
        if (atUserSuffix) {
            if (data.msgtype && data.msgtype !== "text") {
                atUserSuffix = `\n<font color=#007fff>${atUserSuffix}</font>`;
            } else {
                atUserSuffix = `\n${atUserSuffix}`;
            }
        }
        body.msgtype = data.msgtype;
        switch (data.msgtype) {
            case "markdown": {
                body.markdown = {
                    title: data.title || "",
                    text: data.content + atUserSuffix,
                };
                break;
            }
            case "actionCard": {
                body.actionCard = {
                    title: data.title || "",
                    text: data.content + atUserSuffix,
                };
                const btns = data.btns || [];
                if (btns.length) {
                    if (btns.length === 1) {
                        body.actionCard.singleTitle = btns[0].title;
                        body.actionCard.singleURL = btns[0].url;
                    } else {
                        body.actionCard.btnOrientation = "1";
                        body.actionCard.btns = btns.map((v) => {
                            return {
                                title: v.title,
                                actionURL: v.url,
                            };
                        });
                    }
                }
                break;
            }
            case "feedCard": {
                body.feedCard = {};
                const btns = data.btns || [];
                if (btns.length) {
                    body.feedCard.links = btns.map((v) => {
                        return {
                            title: v.title,
                            messageURL: v.url,
                            picURL: v.image,
                        };
                    });
                }
                break;
            }
            default: {
                // content 中必须要有关键词
                body.text = { content: data.content };
                break;
            }
        }
        let webhook = data.webhook;
        // 使用 加签
        if (data.secret) {
            const timestamp = Date.now();
            const str2sign = `${timestamp}\n${data.secret}`;
            const sign = encodeURIComponent(
                require("node:crypto")
                    .createHmac("sha256", data.secret)
                    .update(str2sign)
                    .digest("base64"),
            );
            webhook += `&timestamp=${timestamp}&sign=${sign}`;
        }
        const ret = await doRequest({
            target: {
                url: webhook,
            },
            options: {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                },
                json: body,
            },
        });
        return ret;
    }

    getRequireContent() {
        return true;
    }
}

module.exports = {
    DingtalkCommand,
};
